import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-09-26
 * Time:20:43
 */
public class MyAtomicInteger {
    //基于 AtomicInteger 实现多线程自增同一个变量
    //AtomicInteger:原子类
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger count = new AtomicInteger();

        Thread t1 = new Thread(()->{

            for (int i = 0; i < 5_0000; i++) {
                //相当于 count++
                count.getAndIncrement();
//                //相当于 ++count
//                count.incrementAndGet();
//                //相当于 count--;
//                count.getAndDecrement();
//                //相当于 --count;
//                count.decrementAndGet();
            }

        });

        Thread t2 = new Thread(()->{

            for (int i = 0; i < 5_0000; i++) {
                //相当于 count++
                count.getAndIncrement();
//                //相当于 ++count
//                count.incrementAndGet();
//                //相当于 count--;
//                count.getAndDecrement();
//                //相当于 --count;
//                count.decrementAndGet();
            }

        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
        System.out.println(count);
    }
}
